package com.interrupt;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

@Slf4j
public class InterruptPark {


    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {

            log.debug("开始运行");

            LockSupport.park();


            log.debug("继续运行");

            log.debug("中断状态：{}", Thread.currentThread().isInterrupted());

            // park() 只有当 当前线程 的 interrupt flag 为 false 时，才会阻塞线程
            LockSupport.park();

            log.debug("再次调用 park()");

            log.debug("清除线程的中断标记：{}",Thread.interrupted());


            LockSupport.park();

            log.debug("第二次 unpark()");

        }, "线程1");


        thread.start();

        TimeUnit.SECONDS.sleep(1);

        log.debug("执行打断：{}",thread.getName());
        thread.interrupt();

        TimeUnit.SECONDS.sleep(1);
        log.debug("第二次中断线程");
        thread.interrupt();
    }
}
